package com.moss.system.service.impl;

import com.moss.common.entity.SysCompanyEntity;
import com.moss.common.entity.SysSampleEntity;
import com.moss.common.entity.SysSampleItemEntity;
import com.moss.common.entity.SysSampleOrgEntity;
import com.moss.common.exception.ServiceException;
import com.moss.common.util.IdUtil;
import com.moss.system.mapper.*;
import com.moss.system.service.SampleService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SampleServiceImpl implements SampleService {

    @Resource
    SampleMapper sampleMapper;

    @Resource
    SampleDeliverMapper sampleDeliverMapper;

    @Resource
    SampleItemMapper sampleItemMapper;


    @Override
    public List<SysSampleEntity> list(SysSampleEntity sample) {
        return sampleMapper.selectList(sample);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveSample(SysSampleEntity sample) {
        String sampleId = IdUtil.randomUUID();
        sample.setSampleId(sampleId);
//        this.handleSampleItem(sample);
        sampleMapper.insert(sample);
        sampleItemMapper.insert(sample.getItemList());
        if ("deliver".equals(sample.getSampleType())) {
            sampleDeliverMapper.insert(sample.getDeliver());
        }
    }


    @Override
    public SysSampleEntity getSample(String sampleId) {
        return sampleMapper.selectById(sampleId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateSample(SysSampleEntity sample) {
//        this.handleSampleItem(sample);
        sampleMapper.updateById(sample);
        sampleItemMapper.deleteById(sample.getSampleId());
        sampleItemMapper.insert(sample.getItemList());
    }


    @Override
    public void deleteSample(String[] sampleIds) {
        sampleMapper.deleteById(sampleIds);
    }


    @Override
    public boolean checkDelete(String[] sampleIds) {
        return sampleMapper.checkDelete(sampleIds);
    }
}
